package com.minivision.fdiot;

import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.common.collect.Lists;
import com.minivision.fdiot.common.CommonConstants;
import com.minivision.fdiot.common.RestResult;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2 {

  @Bean
  public Docket createRestApi() {
    List<Parameter> parameters = Lists.newArrayList();
    ParameterBuilder pb = new ParameterBuilder();
    pb.name(CommonConstants.AUTH_HEADER)
      .description("访问令牌，以“appKey{appSecret:timestamp}”拼接成的字符串使用UTF-8编码后再用MD5加密得到的字符串，" 
          + "示例：org.springframework.util.DigestUtils.md5DigestAsHex((appKey + \"{\" + appSecret + \":\" + timestamp + \"}\").getBytes(\"UTF-8\"))")
      .modelRef(new ModelRef("String")).parameterType("header").required(true).build();
    parameters.add(pb.build());
    return new Docket(DocumentationType.SWAGGER_2)
        .globalOperationParameters(parameters)
        .genericModelSubstitutes(RestResult.class)
        .apiInfo(apiInfo())
        .useDefaultResponseMessages(false)
        .forCodeGeneration(true)
        .select()
        .apis(RequestHandlerSelectors.basePackage("com.minivision.fdiot.rest"))
        .paths(PathSelectors.any())
        .build();
  }

  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
        .title("小视人脸智能设备云平台")
        .description("小视人脸智能设备云平台 RESTful APIs")
        .contact(new Contact("PanXinmiao", null, "panxinmiao@minivision.cn"))
        .version("1.0")
        .build();
  }

}